package abq2.jic.jic.plugin.operate.library;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AIBorrowBook implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> param) {
        Map<String , String> result = new HashMap<>();
        if (action.equals("AI-Borrow-Book")){
            int number = Integer.parseInt(param.get("number"));
            String type = param.get("type");
            DynamicObject newBorrow = BusinessDataServiceHelper.newDynamicObject("abq2_books_borrow");
            //生成随机编码
            StringBuilder sb1 = new StringBuilder();
            sb1.append("BookBorrow-");
            for (int j = 1; j <= 10; j++) {
                int ascii = 48 + (int) (Math.random() * 9);
                char c = (char) ascii;
                sb1.append(c);
            }

            //设置单据的基本信息（非单据体）
            newBorrow.set("billno",sb1);
            newBorrow.set("creator", RequestContext.get().getCurrUserId());
            newBorrow.set("billstatus","A");
            newBorrow.set("abq2_datefield", new Date());
            newBorrow.set("abq2_textfield222","未同意");
            DynamicObject myStuInfo = BusinessDataServiceHelper.loadSingle("abq2_tk_student_info",
                    "creator,abq2_textfield,abq2_textfield8,abq2_textfield5"
                    , new QFilter[]{new QFilter("creator", QCP.equals, RequestContext.get().getCurrUserId())});
            newBorrow.set("abq2_textfield3",myStuInfo.getString("abq2_textfield8"));
            newBorrow.set("abq2_textfield",myStuInfo.getString("abq2_textfield"));
            newBorrow.set("abq2_phone",myStuInfo.getString("abq2_textfield5"));
            DynamicObjectCollection books = newBorrow.getDynamicObjectCollection("entryentity");

            DynamicObject[] books_infos = BusinessDataServiceHelper.load("abq2_books_info", "group,name",
                    new QFilter[]{new QFilter("group.name", QCP.equals, type)}
            );
            JSONArray jsonArray = new JSONArray();
            int i = 0;
            for (DynamicObject books_info : books_infos) {
                if (i == number){
                    break;
                }
                JSONObject jsonObject = new JSONObject();
                DynamicObject book = books.addNew();
                book.set("abq2_basedatafield",books_info.getPkValue());
                jsonObject.put("bookName",books_info.getString("name"));
                jsonArray.add(jsonObject);
                i++;
            }
            SaveServiceHelper.saveOperate("abq2_books_borrow", new DynamicObject[] {newBorrow}, null);
            Long pkId = (Long) newBorrow.getPkValue();
            String targetForm = "bizAction://currentPage?gaiShow=1&selectedProcessNumber=processNumber&gaiAction=showBillForm&gaiParams={\"appId\":\"abq2_jic_s\",\"billFormId\":\"abq2_books_borrow\",\"billPkId\":\""+pkId+"\"}&title=图书借阅 &iconType=bill&method=bizAction";
            System.out.println(targetForm);
            result.put("formUrl", targetForm);
            result.put("booksInfo",jsonArray.toJSONString());
        }
        return result;
    }
}
